home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / vr / vrrgmac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-23  |  10.7 KB  |  510 lines

  1. /*
  2.  *
  3.  *      Virtual Graphics Kernel Macintosh Real Graphics Interface
  4.  *                          (rgmac.c)
  5.  *
  6.  *   National Center for Supercomputing Applications
  7.  *      by Gaige B. Paulsen
  8.  *
  9.  *    This file contains the macintosh real screen calls for the NCSA
  10.  *  Virtual Graphics Kernel.
  11.  *
  12.  *    Following are the Per Device calls:
  13.  *
  14.  *   MacRGbell()                    - Ring window's bell
  15.  *   MacRGhidecur()                 - Turn the graphics cursor off in window
  16.  *   MacRGshowcur()                 - Turn the graphics cursor on in window
  17.  *   MacRGpencolor( r,g,b)             - Set the pencolor to 16b R,G,B
  18.  *   MacRGraster( p,x1,y1,x2,y2,wid)- Plot raster in rect @(x1,y1,x2,y2) with wid @ p
  19.  *   MacRGline( x1,y2,x2,y2)        - Draw a line (x1,y1) - (x2,y2)
  20.  *   MacRGlinestyle( n)              - Set line style to n
  21.  *   MacRGpoint( x1,y1)                - Plot a point at (x1,y1)
  22.  *   MacRGfill( x1,y1,x2,y2)        - Fill bounded rectange (x1,y1)-(x2,y2) with pencolor
  23.  *   MacRGcopy( x1,y1,x2,y2,x3,y3,x4,y4)- 
  24.  *   MacRGmap( offset,count,data)   - 
  25.  *
  26.  *
  27.  *  WARNING, WARNING!
  28.  *  Gaige has this cute idea about how to do "subwindows" of real windows by shifting
  29.  *  the window number by 4 bits (MAC_WINDOW_SHIFT).  Then, the remainder is the
  30.  *  sub-window number.  It will probably work, but you MUST keep the shifted and
  31.  *  non-shifted numbers straight.  For example, MacRGdestroy() and MacRGremove() take
  32.  *  different uses of the window number right now.  
  33.  *
  34.  *
  35.  *  Macintosh only Routines:
  36.  *
  37.  *      Version Date    Notes
  38.  *      ------- ------  ---------------------------------------------------
  39.  *        0.5     880912    Initial Coding -GBP
  40.  *      1.0     890216  Minor fixes for 1.0 - TKK
  41.  */
  42.  
  43. #define __ALLNU__
  44. #include <Events.h>
  45. #include <Controls.h>
  46. #include <OSUtils.h>
  47. #include <Dialogs.h>
  48. #include <Memory.h>
  49. #include <Quickdraw.h>
  50. #include <Palette.h>
  51. #include <Windows.h>
  52. #include <Menus.h>
  53. #include <Fonts.h>
  54. #include <Scrap.h>
  55.  
  56. #define RG_MASTER
  57.  
  58. #include <stdio.h>
  59. #include <string.h>
  60.  
  61. #include "RGrout.h"
  62. #include "mpw.h"
  63. #include "configrec.h"
  64. #include "maclook.h"
  65. #include "vdevice.h"
  66. #include "rr.h"
  67. #include "vr.h"
  68. #include "netevent.h"
  69.  
  70. #define MAX_MAC_RGS    8
  71. #define MAX_MAC_SUB 16
  72. #define MAC_WINDOW_SHIFT    4        /* Bits shifted */
  73. #define    MAC_SUB_MASK    0xf            /* Bits maksed */
  74.  
  75. extern unsigned char tempspot[];
  76.  
  77. typedef struct MacWindow {
  78.     VDevice            vdev;            /* virtual device to draw in, has its own colors */
  79.     WindowPtr         window;            /* My Window  (0L if not in use ) */
  80.     PaletteHandle     palette;        /* My Palette */
  81.     char            title[256];        /* Title string */
  82.     
  83.     
  84.     Point            size;            /* My height and width */
  85.     Rect            subs[MAX_MAC_SUB];/* Rectangles of my subwindows [0,0,0,0] if not in use */
  86.     } MacWindow;
  87.  
  88. MacWindow *MacRGs;
  89.  
  90. int MacRGbell(), MacRGhidecur(), MacRGshowcur(), MacRGpencolor(), MacRGraster(), MacRGline(),
  91.         MacRGlinestyle(), MacRGpoint(), MacRGfill(), MacRGmap();
  92.  
  93. int RGwn=0;                            /* Window number in use */
  94. int RGsub=0;                        /* Sub-Window Number in use */
  95.  
  96. void MacRGinit
  97.   (
  98.     void
  99.   )
  100. {
  101.     int i;
  102.     
  103.     MacRGs= (MacWindow *)NewPtr( MAX_MAC_RGS * sizeof(MacWindow));
  104.     for (i=0;i<MAX_MAC_RGS;i++)
  105.         MacRGs[i].window=NULL;
  106. }
  107.  
  108. /************************************************************
  109.  * MacRGnewwindow( name, x1, y1, x2, y2)        -            *
  110.  *        make a new subwindow to wn                            *
  111.  ************************************************************/
  112.  
  113. int MacRGnewwindow
  114.   (
  115.     char *name,
  116.     int x1,
  117.     int y1,
  118.     int x2,
  119.     int y2
  120.   )
  121. {
  122.     int w,i ;
  123.     extern int RScolor;
  124.     Rect wDims;
  125.     register char *p;
  126.     RGBColor curcol;
  127.  
  128.     p = (char *)NewPtr(1000 + (x2-x1+5) *(y2-y1+5));
  129.     if (!p) {
  130. #ifdef MPW
  131.         OtherError("Raster window create error.","Not enough memory to open.");
  132. #endif
  133.         return(-1);
  134.         }
  135.     else
  136.         DisposPtr(p);
  137.     
  138.     for (w=0; w<MAX_MAC_RGS && MacRGs[w].window; w++);
  139.     
  140.     if (w>= MAX_MAC_RGS)
  141.         return( -1);
  142.         
  143.     if ((x2 - x1) & 1)                        /* odd width, must be even */
  144.         x2++;
  145.     
  146.     SetRect( &wDims,  x1+40, y1+40, x2+40, y2+40);
  147.         
  148.     strcpy( MacRGs[w].title, name);                        /* Copy of the name */
  149.     
  150.     if (!RScolor)        /* Borrow from RS */
  151.         return(-1);
  152. /*        MacRGs[w].window=newwindow(0L, &wDims, name, TRUE, 4, -1L, TRUE, (long) w); */
  153.     else {
  154.         int i;
  155.         
  156.         MacRGs[w].window=newcwindow(NULL, &wDims, name, TRUE, 4, (WindowPtr) -1L,
  157.             TRUE, (long) w);
  158.  
  159.  
  160.     MacRGs[w].vdev.bp  = (char *)NewPtr( (x2-x1+5) *(y2-y1+5));
  161.     if (!MacRGs[w].vdev.bp) putln("Window has no bitmap...");
  162.     else {
  163.         SetRect(&MacRGs[w].vdev.bounds, 0,0, x2-x1, y2-y1);
  164.         InitVDevice(&MacRGs[w].vdev);        /* get vdevice going */
  165.     
  166.     
  167.         MacRGs[w].palette = NewPalette( 256, NULL, pmTolerant, 0);
  168.         
  169.         for (i=0; i<256; i++) {                /* load with grey-scale */
  170.             curcol.red = i<<8;
  171.             curcol.green = i<<8;
  172.             curcol.blue = i<<8;
  173.             SetEntryColor( MacRGs[w].palette, i, &curcol);
  174.             }
  175.         
  176.         SetPalette( MacRGs[w].window, MacRGs[w].palette, TRUE);
  177.         ActivatePalette( MacRGs[w].window);
  178.     
  179.         ColorVDevice(&MacRGs[w].vdev,MacRGs[w].palette);
  180.         }
  181.     }
  182.     
  183.  
  184.     if (!MacRGs[w].window) {
  185.         putln("Couldn't open Real Window");
  186.         return(-2);
  187.         }
  188.         
  189.     MacRGs[w].size.h = x2-x1;
  190.     MacRGs[w].size.v = y2-y1;
  191.     for (i=0; i<MAX_MAC_SUB; i++)
  192.         SetRect( &MacRGs[w].subs[i], 0,0,0,0);            /* Reset the subs */
  193.     RGwn = w;
  194.     RGsub= 0;
  195.     return (w << MAC_WINDOW_SHIFT);
  196. }
  197.  
  198. /****************************************
  199.  * MacRGsubwindow(wn)        -            *
  200.  *        make a new subwindow to wn        *
  201.  ****************************************/
  202.  
  203. int MacRGsubwindow
  204.   (
  205.     int wn
  206.   )
  207. {
  208.     return (wn);
  209. }
  210.  
  211. /****************************************
  212.  * MacRGsetwindow(wn)        -            *
  213.  *        set the drawing window to wn    *
  214.  ****************************************/
  215.  
  216. void MacRGsetwindow
  217.   (
  218.     int wn
  219.   )
  220. {
  221.     int w = wn >> MAC_WINDOW_SHIFT;
  222.     
  223.     if (!MacRGs[w].window)
  224.         return;
  225.     
  226.     SetPort( MacRGs[w].window);
  227.     RGwn = w;
  228.     RGsub= wn & MAC_SUB_MASK;
  229.     
  230.     /* Optionally set the clip region */
  231. }
  232.  
  233. /****************************************
  234.  * MacRGdestroy(wn)        -                *
  235.  *        destroy window wn                *
  236.  ****************************************/
  237. void MacRGdestroy
  238.   (
  239.     int wn
  240.   )
  241. {
  242.     
  243.     sprintf(tempspot,"destroy: %d", wn); putln(tempspot);
  244.     if (!MacRGs[wn].window)
  245.         return;
  246.     
  247.     VRdestroy((union arg *) &MacRGs[wn].title);
  248. }
  249.     
  250. /****************************************
  251.  * MacRGremove(wn)        -                *
  252.  *        destroy window wn                *
  253.  ****************************************/
  254. void MacRGremove
  255.   (
  256.     int wn
  257.   )
  258. {
  259.     CGrafPtr cgp;
  260.     int w = wn>> MAC_WINDOW_SHIFT;
  261.     
  262.     if (!MacRGs[w].window)
  263.         return;
  264.     
  265.     TrashVDevice(&MacRGs[w].vdev);
  266.     
  267.     if (MacRGs[w].vdev.bp) 
  268.         DisposPtr(MacRGs[w].vdev.bp);
  269.     
  270.     cgp = (CGrafPtr) MacRGs[w].window;                    /* unseed window color table */
  271.     (*(*(cgp->portPixMap))->pmTable)->ctSeed = GetCTSeed();
  272.     
  273.     DisposeWindow( MacRGs[w].window);        /* Get rid of the actual window */
  274.  
  275.     if (MacRGs[w].palette)
  276.         DisposePalette( MacRGs[w].palette);
  277.         
  278.     MacRGs[w].palette = NULL;
  279.     MacRGs[w].window = NULL;
  280.     sprintf(tempspot,"take away: %d", w); putln(tempspot);
  281. }
  282.  
  283. int MacRGfindwind
  284.   (
  285.     WindowPtr wind
  286.   )
  287. {
  288.     int i=0;
  289.     
  290.     if (!wind)
  291.         return(-2);
  292.     
  293.     while (i<MAX_MAC_RGS && wind != MacRGs[i].window)
  294.         i++;
  295.     if (i==MAX_MAC_RGS)
  296.         return(-1);
  297.  
  298.     return( i);
  299. }
  300.  
  301. /************************************************************************************/
  302. /* MacRGcopy
  303. *  Copybits the image window into the clipboard.
  304. *  
  305. */
  306.     RGBColor icrwhite = { 0xffff,0xffff,0xffff },
  307.             icrblack = { 0,0,0}; 
  308.             
  309. void MacRGcopy
  310.   (
  311.     WindowPtr wind
  312.   )
  313. {
  314.     Rect copysize,copyfrom;
  315.     long len,wn;
  316.     PicHandle picture;
  317.     CGrafPtr hidep;
  318.         
  319.     if (( wn= MacRGfindwind( wind)) <0)
  320.         return;                        /* Couldn't do it */
  321.  
  322.     hidep = &MacRGs[wn].vdev.vport;
  323.     copyfrom = MacRGs[wn].vdev.bounds;
  324.             
  325.     SetPort(wind);
  326.     
  327.     copysize = copyfrom;                        /* boundary of drawing area */
  328.  
  329.     picture= OpenPicture(©size);
  330.  
  331.     ClipRect(©size);
  332.     
  333. /*    RGBBackColor(&icrwhite);
  334.     RGBForeColor(&icrblack); */
  335.  
  336.     ForeColor( blackColor);
  337.     BackColor( whiteColor);
  338.     HLock((Handle) hidep->portPixMap);
  339.                 
  340.     CopyBits((BitMap *) (*(hidep->portPixMap)), &wind->portBits,
  341.             ©from, ©size, srcCopy, NULL); 
  342.             
  343.     HUnlock((Handle) hidep->portPixMap);
  344.  
  345.     ClosePicture();
  346.     
  347. /*
  348. *  put the PICT into the scrap manager
  349. */
  350.     len = GetHandleSize((Handle) picture);
  351.     HLock((Handle) picture);
  352.     ZeroScrap();
  353.     PutScrap( len, 'PICT', (Ptr) *picture);
  354.     HUnlock((Handle) picture);
  355.     KillPicture(picture);
  356.  
  357. }
  358.  
  359. int MacRGupdate
  360.   (
  361.     WindowPtr wind
  362.   )
  363. {
  364.     int wn;
  365.     Rect cbRect;
  366.     
  367.     if (( wn= MacRGfindwind( wind)) <0)
  368.         return(-1);                        /* Couldn't do it */
  369.  
  370.      SetPort(wind);
  371.     ForeColor( blackColor);
  372.     BackColor( whiteColor);
  373.     BeginUpdate(wind);
  374.         /* EraseRect( &wind->portRect); */
  375.         HLock((Handle) MacRGs[wn].vdev.vport.portPixMap);
  376.         cbRect = MacRGs[wn].vdev.bounds;
  377.         CopyBits((BitMap *) *MacRGs[wn].vdev.vport.portPixMap,&(wind->portBits), 
  378.             &cbRect,&cbRect, srcCopy, NULL);
  379.         HUnlock((Handle) MacRGs[wn].vdev.vport.portPixMap);
  380.     EndUpdate( wind);
  381.     return(0);
  382. }
  383.  
  384.     
  385. /****************************************
  386.  * MacRGsetdevice()        -                *
  387.  *        Set the RG's to MacWindow stuff *
  388.  ****************************************/
  389.  
  390. void MacRGsetdevice
  391.   (
  392.     void
  393.   )
  394. {
  395.     RGbell             = MacRGbell;
  396.     RGhidecur         = MacRGhidecur;
  397.     RGshowcur         = MacRGshowcur;
  398.     RGpencolor        = MacRGpencolor;
  399.     RGraster        = MacRGraster;
  400.     RGline            = MacRGline;
  401.     RGlinestyle        = MacRGlinestyle;
  402.     RGpoint            = MacRGpoint;
  403.     RGfill            = MacRGfill;
  404.     RGcopy            = (int (*)()) MacRGcopy;
  405.     RGmap            = MacRGmap;
  406. }
  407.  
  408. /**************************** Hereafter lie the graphics routines ************************/
  409.  
  410. MacRGbell( )
  411. {
  412.     /* Ring bell for wn */
  413. }
  414.  
  415. MacRGhidecur( )
  416. {
  417.     /* hide graphics cursor in wn */
  418. }
  419.  
  420. MacRGshowcur( )
  421. {
  422.     /* show graphics cursor in  wn */
  423. }
  424.  
  425. MacRGpencolor( r, g, b )
  426. {
  427. #pragma unused(r, g, b)
  428.     /* set pen color for wn */
  429. }
  430.  
  431. MacRGraster( data, x1,y1,x2,y2, rowbytes)
  432. char *data;
  433. int x1,y1,x2,y2,rowbytes;
  434. {
  435.     Rect tr;
  436.     register char *p;
  437.     register int i;
  438.     
  439.     if (!MacRGs[RGwn].window)
  440.         return(-1);
  441.         
  442.     SetPort( MacRGs[ RGwn].window);
  443.     
  444.     if (MacRGs[RGwn].vdev.bp) {                /* If we have off-screen buffer */
  445.  
  446.         p = MacRGs[RGwn].vdev.bp + MacRGs[RGwn].size.h*y1 + x1;  /* start point */
  447.         for (i=0; i<rowbytes; i++)
  448.             *p++ = *data++;
  449.  
  450.         SetRect(&tr, x1,y1, x2+1,y2+1);
  451.         InvalRect(&tr);
  452.     }
  453.     
  454.     return(0);
  455. }
  456.  
  457. MacRGline( x1,y1,x2,y2)
  458. {
  459. #pragma unused(x1, y1, x2, y2)
  460.     /* draw a line in  wn */
  461. }
  462.  
  463. MacRGlinestyle( style )
  464. {
  465. #pragma unused(style)
  466.     /* set the linestyle for  wn */
  467. }
  468.  
  469. MacRGpoint( x,y )
  470. {
  471. #pragma unused(x, y)
  472.     /* set a point in wn */
  473. }
  474.  
  475. MacRGfill( x1,y1,x2,y2 )
  476. {
  477. #pragma unused(x1, y1, x2, y2)
  478.     /* fill a region in wn */
  479. }
  480.  
  481. MacRGcopyrgn( x1,y1,x2,y2, x3,y3,x4,y4 )
  482. {
  483. #pragma unused(x1, y1, x2, y2, x3, y3, x4, y4)
  484.     /* copy one region to another within wn */
  485. }
  486.  
  487. MacRGmap( start,length,data)
  488. char *data;
  489. int start,length;
  490. {
  491.     int i;
  492.     RGBColor curcol;
  493.     
  494.     for (i=start; i<start+length; i++) {        
  495.         curcol.red = (*data++)<<8;
  496.         curcol.green = (*data++)<<8;
  497.         curcol.blue = (*data++)<<8;
  498.         SetEntryColor( MacRGs[RGwn].palette, i, &curcol);
  499.     }
  500.     
  501.     SetPalette( MacRGs[RGwn].window, MacRGs[RGwn].palette, TRUE);
  502.     ActivatePalette( MacRGs[RGwn].window);
  503.  
  504.     ColorVDevice(&MacRGs[RGwn].vdev,MacRGs[RGwn].palette);
  505.     
  506.     sprintf(tempspot, "Palette[%d,%d]",start,length);
  507.     putln(tempspot);
  508. }
  509.         
  510.